package pilgun.Multithread.CircularBuffer;

public class CircularBufferImpl implements CircularBuffer{
	final private int len;
	final String[] arr;
	private int start = 0;
	private int occupied = 0;
	
	
	public CircularBufferImpl(int len) {
		this.len = len;
		arr = new String[len];
	}
	
	public synchronized  void put (String elem) throws InterruptedException{
		while (len == occupied){
			wait();

		}
		int end = (start + occupied) % (len);
		occupied++;
		arr[end] = elem;
		notifyAll();
	}
	
	public synchronized  String get () throws InterruptedException{
		while (occupied == 0){
			wait();
		}
		occupied--;
		String elem = arr[start];
		start = (start+1)%len;
		notifyAll();
		return elem;
	}
	
	
	
}
